package com.xinsoft.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinsoft.common.exception.BDException;
import com.xinsoft.constant.Const;
import com.xinsoft.entity.dto.QuaPatrolPlanDTO;
import com.xinsoft.entity.dto.QuaPatrolPlanParam;
import com.xinsoft.entity.po.*;
import com.xinsoft.service.*;
import com.xinsoft.mapper.QuaPatrolPlanMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
* @author lijie
* @description 针对表【qua_patrol_plan(质量巡检计划)】的数据库操作Service实现
* @createDate 2023-08-01 12:33:49
*/
@Service
public class QuaPatrolPlanServiceImpl extends ServiceImpl<QuaPatrolPlanMapper, QuaPatrolPlan>
    implements QuaPatrolPlanService{
    @Resource
    SysDocumentNumService sysDocumentNumService;

    @Resource
    QuaPatrolPlanMapper quaPatrolPlanMapper;

    @Resource
    QuaPatrolPlanDetailService detailService;

    @Resource
    SysUserService userService;

    @Resource
    SysDeptService deptService;

    @Resource
    QuaPatrolStandardService standardService;

    @Override
    public void tempSave(QuaPatrolPlanDTO quaPatrolPlanDTO) {
        // 暂存有新增和编辑场景
        if (quaPatrolPlanDTO.getId() == null) {
            quaPatrolPlanDTO.setPlanStatus(Const.PLAN_STATUS_WAIT);
            // 更新计划单号
            sysDocumentNumService.incrementLastSerialByDocumentId(Const.DOCUMENT_ID.QUA_PATROL_PLAN_DOCUMENT);
            quaPatrolPlanMapper.insert(quaPatrolPlanDTO);
            // 解析计划设备项目关系表
            if (quaPatrolPlanDTO.getQuaStandardList().size() > 0) {
                this.dealList(quaPatrolPlanDTO);
            } else {
                throw new BDException("操作失败，不包含设备巡检项目集合！");
            }
        } else {
            // 解析计划设备项目关系表
            quaPatrolPlanDTO.setPlanStatus(Const.PLAN_STATUS_WAIT);
            if (quaPatrolPlanDTO.getQuaStandardList().size() > 0) {
                detailService.remove(new QueryWrapper<QuaPatrolPlanDetail>().eq("patrol_plan_id", quaPatrolPlanDTO.getId()));
                this.dealList(quaPatrolPlanDTO);
            } else {
                throw new BDException("操作失败，不包含设备项目集合！");
            }
            quaPatrolPlanMapper.updateById(quaPatrolPlanDTO);
        }
    }

    @Override
    public void submit(QuaPatrolPlanDTO quaPatrolPlanDTO) {
// 提交场景
        if (quaPatrolPlanDTO.getId() == null) {
            quaPatrolPlanDTO.setPlanStatus(Const.PLAN_STATUS_NORMAL);
            sysDocumentNumService.incrementLastSerialByDocumentId(Const.DOCUMENT_ID.PATROL_PLAN_DOCUMENT);
            quaPatrolPlanMapper.insert(quaPatrolPlanDTO);
            // 解析计划设备项目关系表
            if (quaPatrolPlanDTO.getStandardList().size() > 0) {
                this.dealList(quaPatrolPlanDTO);
            } else {
                throw new BDException("操作失败，巡检项目集合！");
            }
        } else {
            quaPatrolPlanDTO.setPlanStatus(Const.PLAN_STATUS_NORMAL);
            // 解析计划设备项目关系表
            if (quaPatrolPlanDTO.getQuaStandardList().size() > 0) {
                detailService.remove(new QueryWrapper<QuaPatrolPlanDetail>().eq("patrol_plan_id", quaPatrolPlanDTO.getId()));
                this.dealList(quaPatrolPlanDTO);
            } else {
                throw new BDException("操作失败，不包含设备项目集合！");
            }
            quaPatrolPlanMapper.updateById(quaPatrolPlanDTO);
        }
        // 根据提交的计划去创建定时任务测试
//        QuartzUtil.addJobTask(PatrolPlanJob.class, cusPatrolPlanDTO.getPlanStartTime(), cusPatrolPlanDTO.getPlanEndTime(), cusPatrolPlanDTO.getIntervalHour(), cusPatrolPlanDTO.getId());
    }

    @Override
    public Object findPageListByParam(QuaPatrolPlanParam param) {
        //分页
        Page<QuaPatrolPlan> page = new Page<QuaPatrolPlan>(param.getCurrentPage(), param.getPageSize());
        IPage<QuaPatrolPlan> resultPage = quaPatrolPlanMapper.findListByParam(page, param);
        //返回值
        return resultPage;
    }

    @Override
    public QuaPatrolPlan getByInfoById(Integer id) {
        QuaPatrolPlan quaPatrolPlan = quaPatrolPlanMapper.selectById(id);
        // 组装负责人姓名，电话，部门
        SysUser user = userService.getById(quaPatrolPlan.getPatrolPersonId());
        if (user != null) {
            quaPatrolPlan.setPlanPersonName(user.getRealName());
            quaPatrolPlan.setPlanPersonDept(deptService.getById(user.getDeptId()).getDeptName());
            quaPatrolPlan.setPlanPersonTel(user.getMobile());
        }
        // 组装巡检明细
        List<QuaPatrolPlanDetail> list = detailService.list(new QueryWrapper<QuaPatrolPlanDetail>().eq("patrol_plan_id", id));
        List<Integer> detailIds = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            detailIds.add(list.get(i).getPatrolStandardId());
        }
        if (list.size() > 0) {
            List<QuaPatrolStandard> patrolStandardList = standardService.listByIds(detailIds);
            quaPatrolPlan.setStandardList(patrolStandardList);
        }
        return quaPatrolPlan;
    }

    private void dealList(QuaPatrolPlanDTO quaPatrolPlanDTO) {
        for (int i = 0; i < quaPatrolPlanDTO.getQuaStandardList().size(); i++) {
            QuaPatrolPlanDetail detail = new QuaPatrolPlanDetail();
            detail.setPatrolPlanId(quaPatrolPlanDTO.getId());
            detail.setPatrolStandardId(quaPatrolPlanDTO.getQuaStandardList().get(i).getId());
            detailService.save(detail);
        }
    }
}




